Thread: Comparing integer char[] elements

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Question Comparing integer char[] elements

    Hey everybody,

    I've got two char arrays which each represent college course names. I want to compare the courses based off the integer part of their name to determine which one is larger, but am not quite sure how to do this. These are the two arrays I've got...

    Code:
    char a[6] = "CS120";
    char b[6] = "CS450";
    I am trying to accomplish this:
    Code:
    if(120 < 450)
    {
         do stuff here
    }
    Any help or suggestions would be greatly appreciated.

    Thanks,
    Chris C.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Find where the number starts.
    2. Use strtol or atoi to turn the rest of the string into an integer.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, tabstops reply applies if you actually need to know what the number is.

    If you just compare the strings (using for example strcmp), then "CS120" will be less than "CS450" (strcmp will return a number less than zero if the first string is "less" than the second string are in that order).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    You could use strcmp(). strcmp will tell you that CS120 comes before CS450 by returning a negative number (or maybe positive- look up the specs for yourself.)
    example:
    Code:
    if (strcmp(a,b)<0) /* Just like if (a<b) */
    {
        stuff
    }
    Last edited by NeonBlack; 04-20-2008 at 03:24 PM. Reason: damn I type slow
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Ok, that makes sense. So I would just need to isolate the number part (i.e. indexes 2-4) of the string and pass that into the atoi() function.

    Is there a easy way to isolate a substring of a string, or would I just need to copy the values to a new string? Something like this:

    Code:
    char a[6] = "CS120";
    char newarray[4];
    
    newarray[0] = a[2];
    newarray[1] = a[3];
    newarray[2] = a[4];
    Thanks,
    Chris C.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you Know that it's always 2 chars to begin with (such as "CS", "AA", "XY" etc), and that the number fills the entire rest of the string, then you could use atoi like this:
    Code:
    int n = atoi(&str[2]);
    Although I would prefer something that uses strtol(), since strtol() is more able to cope with malformed inputs and give you a chance of detecting this - atoi() is very bad at coping with malformed input, and will not tell you at all that it failed, or why...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    atoi requires a pointer to the start of the string, which in your case would be &a[2] (or a+2 if you like the pointer arithmetic).

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Ok, thanks guys. I'll give strcmp() a try first. That sounds like it might work... if not, I'll go the atoi() route.

    Thanks,
    Chris C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  3. integer to character
    By Gil22 in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2005, 09:20 PM
  4. Comparing array elements
    By Tride in forum C Programming
    Replies: 8
    Last Post: 09-13-2003, 12:10 PM
  5. ? comparing elements of a char array
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2001, 07:55 AM